package com.nf.db.handlers.list;

import com.nf.db.DaoException;
import com.nf.db.handlers.ResultSetHandler;

import javax.xml.ws.handler.Handler;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.nf.db.handlers.RowProcessor.DEFAULT_ROWPROCESSOR;

/*
 * 让ResultSetHandler处理结果集，并将结果集转换为List<Object>
 * */
public class ColumnListHandler<T> extends AbstractListHandler<T> {
    private Integer columnIndex;
    private String columnName;


    public ColumnListHandler() {
        this(1, null);
    }

    public ColumnListHandler(Integer columnIndex) {
        this(columnIndex, null);
    }

    public ColumnListHandler(String columnName) {
        this(-1, columnName);
    }

    public ColumnListHandler(Integer columnIndex, String columnName) {
        this.columnIndex = columnIndex;
        this.columnName = columnName;
    }

    @Override
    protected T handlerRow(ResultSet rs) throws SQLException {
        return DEFAULT_ROWPROCESSOR.toScalar(rs, columnName, columnIndex);
    }
}
